package com.nike.amoeba.ui.frg

import android.os.Bundle
import android.view.View
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.Observer
import androidx.lifecycle.viewModelScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.ConvertUtils
import com.google.gson.Gson
import com.kingja.loadsir.core.LoadService
import com.nike.amoeba.R
import com.nike.amoeba.adapter.StockChuAdapter
import com.nike.amoeba.base.BaseFragment
import com.nike.amoeba.bean.DataBean
import com.nike.amoeba.bean.RfidStateBean
import com.nike.amoeba.databinding.FStockChuEpcBinding
import com.nike.amoeba.ext.init
import com.nike.amoeba.ext.loadListData
import com.nike.amoeba.ext.loadServiceInit
import com.nike.amoeba.ext.showLoading
import com.nike.amoeba.mar.eventViewModel
import com.nike.amoeba.viewmodel.AssetModel
import com.nike.amoeba.viewmodel.RfidModel
import com.nike.amoeba.viewmodel.StockManageModel
import com.nike.amoeba.weight.recyclerview.SpaceItemDecoration
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

/**
 * User: Nike
 *  2024/5/13 09:49
 *      入库
 */
class StockChuEpcFrg: BaseFragment<StockManageModel, FStockChuEpcBinding>(){

    val adapter: StockChuAdapter by lazy { StockChuAdapter(arrayListOf()) }

    lateinit var loadsir: LoadService<Any>

    var stockType: Int = 0

    var bean: DataBean? = null

    var stockId: String? = null

    val stockManageModel: StockManageModel by activityViewModels()

    override fun initView(savedInstanceState: Bundle?) {
        arguments?.let {
            mViewModel.batchNo.set(it.getString("batchNo"))
            mViewModel.code.set(it.getString("code"))
            stockType = it.getInt("stockType")
            bean = Gson().fromJson(it.getString("bean") , DataBean::class.java)
            stockId = bean!!.id
        }
        mDatabind.viewmodel = mViewModel
        mDatabind.click = ProxyClick()

        //状态页配置
        loadsir = loadServiceInit(mDatabind.swipeRefresh) {
            loadsir.showLoading()
            mViewModel.onChuRequest(bean!!, stockId)
        }

        mViewModel.viewModelScope.launch(Dispatchers.IO) {
            val configBean = mViewModel.configDao.findFirst()
            configBean.powerType = 0
            mViewModel.configDao.update(configBean)
        }

        mViewModel.save.set(getString(R.string.save))
        setToolBarTitle(getString(R.string.number) + "：" + bean!!.AssetNo!!)

        mDatabind.swipeRefresh.isEnabled = false
        mDatabind.recyclerView.init(LinearLayoutManager(context), adapter).let {
            it.addItemDecoration(SpaceItemDecoration(ConvertUtils.dp2px(20f), ConvertUtils.dp2px(20f), true))
        }
        mViewModel.onChuRequest(bean!!, stockId)
    }

    override fun createObserver() {
        super.createObserver()
        mViewModel.listBean.observe(viewLifecycleOwner, Observer {
            loadListData(it, adapter, loadsir, mDatabind.recyclerView, mDatabind.swipeRefresh, it.pageSize)
            adapter.appendList(it.listData)
            stockManageModel.updateStockManageListCallback.value = true
        })
    }

    inner class ProxyClick() {
        fun save(){
            if (adapter.mFilterList.size == 0)return
            mViewModel.onSave(this@StockChuEpcFrg, adapter.mFilterList, stockId)
        }
    }
}